import { time } from "console";
import { Vector3 } from "three";
import { defaultInitScene } from "../utils/InitScene";
export const enableZoom = (el: HTMLElement = document.body) => {
  var factor = 3;
  var WIDTH = window.innerWidth;
  var HEIGHT = window.innerHeight;
  el.addEventListener("mousewheel", (Event: Event) => {
    const ev = Event as WheelEvent;
    //将鼠标的屏幕坐标转换为NDC坐标
    var mX = (ev.clientX / WIDTH) * 2 - 1;
    var mY = -(ev.clientY / HEIGHT) * 2 + 1;
    //这里定义深度值为0.5，深度值越大，意味着精度越高
    var vector = new Vector3(mX, mY, 0.5);
    const camera = defaultInitScene.camera;
    //将鼠标坐标转换为3D空间坐标
    vector.unproject(camera);
    const controls = defaultInitScene.controls;

    //获得从相机指向鼠标所对应的3D空间点的射线（归一化）
    vector.sub(camera.position).normalize();
    if (ev.deltaY < 0) {
      camera.position.x += vector.x * factor;
      camera.position.y += vector.y * factor;
      camera.position.z += vector.z * factor;
      controls.target.x += vector.x * factor;
      controls.target.y += vector.y * factor;
      controls.target.z += vector.z * factor;
    } else {
      camera.position.x -= vector.x * factor;
      camera.position.y -= vector.y * factor;
      camera.position.z -= vector.z * factor;
      controls.target.x -= vector.x * factor;
      controls.target.y -= vector.y * factor;
      controls.target.z -= vector.z * factor;
    }
  });
};
